/* TODO: Write a program to determine your system's byte ordering */

#include <stdlib.h>
#include <endian.h>
#include <stdio.h>
#include <stdint.h>

void
check_endianess(intptr_t first_byte, intptr_t second_byte)
{
  /* Check endianess */
  if (first_byte < second_byte && *((uint8_t *)first_byte) == 1)
    {
      printf("Little-endian byte order detected\n");
    }
  else
    {
      printf("Big-endian byte order detected\n");
    }
}

int
main(void)
{
  union {
    uint16_t u16;
    uint8_t arr[2];
  } x = { .u16 = 1 };

  intptr_t first_byte = (intptr_t)&x.arr[0];
  intptr_t second_byte = (intptr_t)&x.arr[1];
  printf("%14d\t%14d\n", x.arr[0], x.arr[1]);
  printf("%14p\t%14p\n", first_byte, second_byte);
  check_endianess(first_byte, second_byte);

  x.u16 = htobe16(x.u16);
  check_endianess(first_byte, second_byte);

  return EXIT_SUCCESS;
}
